package com.free4inno.pagesite.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.free4inno.pagesite.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@Controller
public class ResourceController {
    @Value("${zhi.picture.url}")
    private String url;

    @Value("${zhi.theme.name}")
    private String[] themeName;
    @Autowired
    private ResourceService resourceService;


    /**
     * 资源详情页
     * （返回页面）
     */
    @GetMapping("/resource")
    public String resource(@RequestParam(required = true, value = "id") Integer id, Map param) {
        log.info("resource ==== start ==== id:" + id);
        String responseStr = resourceService.getResourceDetail(id);
        JSONObject jsonObject = JSONObject.parseObject(responseStr);
        JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
        JSONObject res = JSONObject.parseObject(JSONObject.parseObject(jsonObject.get("data").toString()).get("resource").toString());
        res.put("createTime",ChanggeTime(JSONObject.parseObject(JSONObject.parseObject(jsonObject.get("data").toString()).get("resource").toString()).get("createTime").toString()));
        res.put("editTime",ChanggeTime(JSONObject.parseObject(JSONObject.parseObject(jsonObject.get("data").toString()).get("resource").toString()).get("editTime").toString()));
        data.put("resource",res);
        jsonObject.put("data",data);
        param.put("res", jsonObject);
        JSONObject resource = JSONObject.parseObject(data.get("resource").toString());
        String text = resource.get("text").toString()
                .replaceAll("src=\"/", "src=\""+url)
                .replaceAll("src=\"../", "src=\""+url);
        param.put("data", text);
        JSONArray attachmentS = JSONArray.parseArray(data.get("attachment").toString());
        Boolean  isFile = attachmentS.size() != 0;
        param.put("isFile", isFile);
        Map<String,String> fileMapS = new HashMap<>();
        for (Object attachment : attachmentS) {
            JSONObject val = JSONObject.parseObject(attachment.toString());
            String fileName = val.get("name").toString();
            String fileUrl = "http://zhi.free4inno.com"+val.get("url").toString();
            fileMapS.put(fileName,fileUrl);
        }
        param.put("fileMapS", fileMapS);
        log.info("resource ==== end ====");
        return "resource/resource";
    }

    public String ChanggeTime(String timeStamp){
        SimpleDateFormat sdf=  new SimpleDateFormat(  "yyyy-MM-dd HH:mm:ss" );
        String sd = sdf.format(  new Date(Long.parseLong(String.valueOf(timeStamp))));        // 时间戳转换成时间
        return sd;
    }

    /**
     * 搜索数据集资源
     * （直接返回openapi调用获得的json字符串）
     */
    @ResponseBody
    @GetMapping("/search")
    public String search(@RequestParam(required = true, value = "queryStr") String queryStr,
                         @RequestParam(required = true, value = "pageNum") Integer pageNum,
                         @RequestParam(required = true, value = "pageSize") Integer pageSize,
                         @RequestParam(required = false, value = "labelNames") String labelNames) {
        log.info("search ==== start ==== queryStr:" + queryStr + " pageNum:" + pageNum + " labelNames:" + labelNames);
        JSONArray labelNamesArray = JSON.parseArray(labelNames);
        labelNamesArray.addAll(Arrays.asList(themeName));
        String responseStr = resourceService.searchPageSite(queryStr, pageNum, pageSize, labelNamesArray);
        log.info("search ==== end ====");
        System.out.println(responseStr);
        return responseStr;
    }
}
